import {Injectable} from '@angular/core';
import {Response} from "@angular/http";
import {BehaviorSubject, Subject} from 'rxjs';
import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";
import {ToolsService} from "@shared/service/tools/tools.service";
import {Observable} from "rxjs/Observable";
import {SNSResult, TicketVO} from '@interface/index';

@Injectable()
export class SharedService {
  public PRO_CITY_URL = '/dict/getDictAreaTree';   // 所有地区
  //  public currentCity: Subject<any> = new Subject();
  public currentCity: BehaviorSubject<any> = new BehaviorSubject({});
  public FRIEND_URLS = '/user/friend';

  constructor(
    public httpService: HttpService,
    public toolsService: ToolsService
  ) {
  }

  // 获取地区数据
  getProvinces() {
    const url = this.PRO_CITY_URL + '?langType=zh_CN';
    return this.httpService.get(url).map((rs) => rs.json());
  };

  GetFeedFriend(followUserId: string, operation: boolean = true) {
    // const data = {
    //   followUserId: followUserId
    // };
    // if (operation) {
    //   const url = this.FRIEND_URLS;
    //   return this.httpService.postUrlencode(url, data).map((res) => res.json());
    // } else {
    //   const url = this.FRIEND_URLS + `?followUserId=${followUserId}`;
    //   return this.httpService.delete(url).map((res: Response) => res.json());
    // }
    let url = this.FRIEND_URLS;
    if (operation) {
      const data = {
        followUserId: followUserId
      };
      return this.httpService.postUrlencode(url, data).map((res) => res.json());
    } else {
      url += `?followUserId=${followUserId}`;
      return this.httpService.delete(url).map((res: Response) => res.json());
    }

  }

  /**
   * @description 获取微信config 签名参数
   * @author zhoukf1349
   * @date 2018/1/31
   * */
  fetchWechatConfig(url: string): Observable<SNSResult<TicketVO>> {
    // console.warn('url=======================================' + url);
    const _url = API.AUTH_GET_WX_CONFIG + '?wxurl=' + url;
   // console.warn('_url=======================================++++++++++++' + url);
    return this.httpService.postWechat(_url, {}).map((res: Response) => res.json());
  };

  fetchShareData(params) {
    const url = API.GUEST_SECOND_SPREAD + '?' + this.toolsService.param(params);
    return this.httpService.get(url).map((res) => res.json());
  };


}
